package com.luyuan.crm.product.web.convert;

import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.luyuan.crm.product.model.ProductType;
import com.luyuan.crm.product.service.ProductTypeService;
import com.ruijie.web.jsf.converter.AbstractConverter;
import com.ruijie.util.StringUtils;

@Component("productTypeConverter")
public class ProductTypeConverter extends AbstractConverter implements  Converter, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ProductTypeService productTypeService;

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		if(StringUtils.hasText(value)){
			ProductType model = this.productTypeService.getProductTypeById(value);
            return model;
        }
        return null;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
		if(object == null) return null;
		ProductType model = (ProductType)object;
		return model.getId().toString();
	}
	
}
